package LoginFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {
    public LoginFrame() {
        // 设置窗口标题
        setTitle("用户登录");
        // 设置窗口大小
        setSize(600, 500);
        // 设置窗口关闭操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 居中显示窗口
        setLocationRelativeTo(null);

        // 创建面板并设置布局管理器
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // 添加边距

        // 创建顶部面板用于放置标题
        JPanel topPanel = new JPanel();
        JLabel titleLabel = new JLabel("用户登录", SwingConstants.CENTER);
        titleLabel.setFont(new Font("宋体", Font.BOLD, 28)); // 增大字体
        topPanel.add(titleLabel);
        panel.add(topPanel, BorderLayout.NORTH);

        // 创建中间面板用于放置用户名和密码输入框
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridBagLayout()); // 使用GridBagLayout以便更灵活地控制布局
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10); // 添加间距

        JLabel usernameLabel = new JLabel("账号：");
        usernameLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        gbc.gridx = 0;
        gbc.gridy = 0;
        centerPanel.add(usernameLabel, gbc);

        JTextField usernameField = new JTextField();
        usernameField.setFont(new Font("宋体", Font.PLAIN, 20));
        usernameField.setPreferredSize(new Dimension(300, 30));
        gbc.gridx = 1;
        gbc.gridy = 0;
        centerPanel.add(usernameField, gbc);

        JLabel passwordLabel = new JLabel("密码：");
        passwordLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        gbc.gridx = 0;
        gbc.gridy = 1;
        centerPanel.add(passwordLabel, gbc);

        JPasswordField passwordField = new JPasswordField();
        passwordField.setFont(new Font("宋体", Font.PLAIN, 20));
        passwordField.setPreferredSize(new Dimension(300, 30));
        gbc.gridx = 1;
        gbc.gridy = 1;
        centerPanel.add(passwordField, gbc);

        panel.add(centerPanel, BorderLayout.CENTER);

        // 创建底部面板用于放置记住密码复选框和登录按钮
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 20, 0));

        JCheckBox rememberPasswordCheckbox = new JCheckBox("记住密码");
        rememberPasswordCheckbox.setFont(new Font("宋体", Font.PLAIN, 20));
        bottomPanel.add(rememberPasswordCheckbox);

        JButton loginButton = new JButton("确定");
        loginButton.setFont(new Font("宋体", Font.PLAIN, 20));
        bottomPanel.add(loginButton);

        panel.add(bottomPanel, BorderLayout.SOUTH);

        // 添加事件监听器到登录按钮
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                char[] passwordChars = passwordField.getPassword();
                String password = new String(passwordChars);

                if ("admin".equals(username) && "123".equals(password)) {
                    JOptionPane.showMessageDialog(LoginFrame.this, "登录成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(LoginFrame.this, "无法登录！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        // 将面板添加到窗口
        add(panel);

        // 设置窗口可见
        setVisible(true);
    }

    public static void main(String[] args) {
        new LoginFrame();
    }
}
